package com.lbmb.mdps.workbench.service.impl;

import com.lbmb.mdps.commons.domain.HistoriesVo;
import com.lbmb.mdps.commons.domain.PurchaseReceive;
import com.lbmb.mdps.commons.domain.PurchaseVo;
import com.lbmb.mdps.commons.domain.UserReceive;
import com.lbmb.mdps.commons.exception.MedicineException;
import com.lbmb.mdps.commons.exception.MedicineTypeException;
import com.lbmb.mdps.commons.exception.PurchaseException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.settings.dao.EmployeeDao;
import com.lbmb.mdps.settings.domain.Employee;
import com.lbmb.mdps.workbench.dao.*;
import com.lbmb.mdps.workbench.domain.*;
import com.lbmb.mdps.workbench.service.PurchaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-06-01 2:16
 * description
 */
@Service("purchaseService")
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    private PurchaseDao purchaseDao;
    @Resource
    private MedicineDao medicineDao;
    @Resource
    private MedicineTypeDao medicineTypeDao;
    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private WarehouseDao warehouseDao;
    @Resource
    private AuditDao auditDao;

    @Override
    public Integer getPurchaseTotal(PurchaseReceive purchaseReceive) throws PurchaseException {
        /*
         * 获得采购记录总条数
         * @author 林文塨
         * @date 2022/6/1 8:22
         * @param [purchaseReceive]
         * @return java.lang.Integer
         */
        Integer total = purchaseDao.getPurchaseTotal(purchaseReceive);

        if (total < 0) {
            throw new PurchaseException("获取采购订单分页总条数失败！");
        }

        return total;
    }

    @Override
    public List<PurchaseVo> purchasePageList(PurchaseReceive purchaseReceive) throws PurchaseException {
        /*
         * 分页获取采购记录
         * @author 林文塨
         * @date 2022/6/1 8:22
         * @param [purchaseReceive]
         * @return java.util.List<com.lbmb.mdps.workbench.domain.Purchase>
         */
        List<PurchaseVo> purchaseVoList = new ArrayList<>();

        List<Purchase> purchaseList = purchaseDao.purchasePageList(purchaseReceive);

        if (purchaseList == null) {
            throw new PurchaseException("分页获取采购订单列表失败！");
        }

        for (Purchase purchase : purchaseList) {

            PurchaseVo purchaseVo = new PurchaseVo();

            /*设置药品*/
            Medicine medicine = medicineDao.getMedicineById(purchase.getMedicine());
            MedicineType medicineType = medicineTypeDao.getMedicineTypeById(medicine.getMedicineType());
            medicine.setMedicineTypeVo(medicineType);

            /*设置基本属性*/
            purchaseVo.setMedicine(medicine);
            purchaseVo.setId(purchase.getId());
            purchaseVo.setNumber(purchase.getNumber());
            purchaseVo.setOrderTime(purchase.getOrderTime());
            purchaseVo.setState(purchase.getState());
            purchaseVo.setAggregateAmount(purchase.getAggregateAmount());

            /*设置下单人*/
            Employee employee = employeeDao.getEmployeeById(purchase.getOrderBy());
            purchaseVo.setOrderBy(employee.getName());

            /*设置供应商*/
            Supplier supplier = supplierDao.getSupplierById(purchase.getSupplier());
            purchaseVo.setSupplier(supplier.getName());

            /*设置仓库*/
            Warehouse warehouse = warehouseDao.getWarehouseById(purchase.getWarehouse());
            purchaseVo.setWarehouse(warehouse.getName());

            /*设置审核时间表*/
            List<Audit> auditList = auditDao.getAuditsByAuditOrderId(purchase.getId());
            List<HistoriesVo> historiesVoList = new ArrayList<>();

            /*获得所有审核时间表*/
            for (Audit audit : auditList) {
                HistoriesVo historiesVo = new HistoriesVo();

                if ("待审核".equals(audit.getState())) {
                    historiesVo.setContent("待审核——操作人（" + employee.getName() + ")");
                    historiesVo.setTimestamp(audit.getAuditTime());
                    historiesVo.setSize("large");
                    historiesVo.setType("primary");
                    historiesVo.setIcon("el-icon-warning");
                    historiesVo.setColor("blue");
                } else if ("已拒绝".equals(audit.getState())) {
                    historiesVo.setContent("已拒绝——操作人（" + employee.getName() + ")");
                    historiesVo.setTimestamp(audit.getAuditTime());
                    historiesVo.setSize("large");
                    historiesVo.setType("primary");
                    historiesVo.setIcon("el-icon-error");
                    historiesVo.setColor("red");
                } else if("已通过".equals(audit.getState())) {
                    historiesVo.setContent("已通过——操作人（" + employee.getName() + ")");
                    historiesVo.setTimestamp(audit.getAuditTime());
                    historiesVo.setSize("large");
                    historiesVo.setType("primary");
                    historiesVo.setIcon("el-icon-success");
                    historiesVo.setColor("green");
                }

                historiesVoList.add(historiesVo);
            }

            purchaseVo.setHistoriesVoList(historiesVoList);

            purchaseVoList.add(purchaseVo);
        }

        return purchaseVoList;
    }

    @Override
    public void editOrderState(UserReceive userReceive) throws PurchaseException {
        /*
         * 更改订单状态，同时增加审核记录
         * @author 林文塨
         * @date 2022/6/1 15:03
         * @param [userReceive]
         * @return void
         */
        int result1 = purchaseDao.editOrderState(userReceive);

        if (result1 != 1) {
            throw new PurchaseException("通过订单失败！");
        }

        Audit audit = new Audit();
        audit.setId(SnowFlakeIdUtil.generateId());
        audit.setState(userReceive.getState());
        audit.setAuditOrder(Long.valueOf(userReceive.getCurrentOrderId()));
        audit.setAuditBy(Long.valueOf(userReceive.getCurrentOrderId()));
        audit.setAuditTime(DateTimeUtil.getSysTime());

        int result2 = auditDao.addAudit(audit);

        if (result2 != 1) {
            throw new PurchaseException("添加审核记录失败！");
        }
    }
}
